package org.wenzi.com;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * @author zhaozuowen
 * @date 2021-10-15 17:50
 */
public class BatchWordCount {

    public static void main(String[] args) throws Exception {
        ExecutionEnvironment ex = ExecutionEnvironment.getExecutionEnvironment();
        DataSource<String> source = ex.fromElements("Flink Spark Hadoop", "Flink Spark Hdfs", "ElasticSearch Storm Hadoop");

        AggregateOperator<Tuple2<String, Integer>> sum = source.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
                String[] tokens = s.toLowerCase().split("\\W+");
                for (String token : tokens) {
                    if (token.length() > 0) {
                        collector.collect(Tuple2.of(token, 1));
                    }
                }
            }
        }).groupBy(0).sum(1);

        sum.print();
    }
}
